﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
namespace HuynhQuangWebsite.Controllers
{
    [HandleError]
    public class ManageUserController : BaseController
    {
        //
        // GET: /userManagerment/
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        public int PageSize = 6;
        #region--Admin--
        public ActionResult IndexUser(int page = 1)
        {
            //  if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOnDashBoard", "DashBoard");
            var model = new HomeAdminListViewModel();
            IEnumerable<Models.User> users = ctx.Users.ToList<User>();
            model.UserList = users.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.UserInfo = new THS.HtmlHelpers.PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = users.Count() };
            return View(model);

        }
        //pagign
        public ActionResult Paging(int page = 1)
        {
            //  if (!IsAuthenticateAdmin())
            //    return RedirectToAction("LogOnDashBoard", "DashBoard");
            var model = new HomeAdminListViewModel();
            IEnumerable<Models.User> users = ctx.Users.ToList<User>();
            model.UserList = users.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.UserInfo = new THS.HtmlHelpers.PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = users.Count() };
            return PartialView("UCUserList",model);

        }
        //
        // GET: /userManagerment/Details/5

        public ActionResult Detail(int id)
        {
            try
            {
                // if (!IsAuthenticateAdmin())
                //  return RedirectToAction("LogOnDashBoard", "DashBoard");
                Models.User adm = ctx.Users.Single(p => p.UserID == id);
                if (adm != null)
                    return View(adm);
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /userManagerment/Create

        public ActionResult Create()
        {
            //// if (!IsAuthenticateAdmin())
            ////   return RedirectToAction("LogOnDashBoard", "DashBoard");
            //User user = new User();
            return View();
        }

        //
        // POST: /userManagerment/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "UserID")] User us)
        {
            // Attempt to register the user


            //if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOnDashBoard", "DashBoard");
            try
            {

                    IEnumerable<Models.User> existuser = ctx.Users.Where(p => p.UserName == us.UserName).ToList();
                    if (existuser.Count() == 0)
                    {
                        EnCrypto enc = new EnCrypto();
                        us.Password = enc.Encrypt(us.Password);
                        us.AACreateDate = DateTime.Now;
                         us.UserIsActive = true;
                        ctx.AddToUsers(us);
                        ctx.SaveChanges();
                        return RedirectToAction("IndexUser");
                    }
                    else
                    {
                        ViewData["Msg"] = "Đã tồn tại tên đăng nhập";
                        return View();

                    }
            }
            catch
            {
                return View("Create");
            }

        }

        //check esxist user name
        public bool CheckName(string userName)
        {
            try
            {
                Models.User user = ctx.Users.Where(p=>p.UserName.Trim()==userName.Trim()).SingleOrDefault() as User;
                if (user != null)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }
        public bool CheckEmail(String email)
        {
            try
            {
                User user = ctx.Users.Where(p => p.Email.Trim() == email.Trim()).SingleOrDefault() as User;
                if (user!=null)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }

        //
        // POST: /userManagerment/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Detail(int id, FormCollection collection)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //  return RedirectToAction("LogOn", "AdminHome");
                Models.User adm = ctx.Users.Where(ad => ad.UserID == id).SingleOrDefault();
                if (adm != null)
                {
                    EnCrypto crt = new EnCrypto();
                    adm.Password = crt.Encrypt(collection["Password"]);
                    ctx.SaveChanges();
                    ViewData["Msg"] = "Cập nhật thành công";
                }
                return RedirectToAction("Detail");
            }
            catch
            {
                ViewData["Msg"] = "Cập nhật không thành công";
                return RedirectToAction("Detail");
            }
        }
        //allow Log On
        public void ActiveUser(int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //  return;
                User adm = ctx.Users.Where(p => p.UserID == id).SingleOrDefault() as User;
               if (adm.UserIsActive)
                   adm.UserIsActive = false;
                else
                   adm.UserIsActive = true;
                ctx.SaveChanges();


            }
            catch
            {
                return;
            }
        }
        //active,deactivate logon
        //deactive
        public ActionResult DeactivateUsers(int id)
        {
            try
            {
                if (!IsAuthenticateAdmin())
                    return RedirectToAction("LogOn", "AdminHome");
                User us = ctx.Users.Where(p => p.UserID != id) as User;
                if (us != null)
                {
                    us.UserIsActive = false;
                    ctx.SaveChanges();
                }
                return RedirectToAction("Index");

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
     
        //
        // GET: /userManagerment/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                if (!IsAuthenticateAdmin())
                    return RedirectToAction("LogOn", "AdminHome");
                User adm = ctx.Users.Where(p => p.UserID == id).SingleOrDefault();
                ctx.Users.DeleteObject(adm);
                ctx.SaveChanges();
                return RedirectToAction("IndexUser");
            }
            catch
            {
                return View();
            }
        }


        #endregion


    }
}
